package com.incar.chuxing.wxapi;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;

import com.incar.chuxing.bean.WxAuthEvent;
import com.incar.chuxing.utils.Logger;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import org.greenrobot.eventbus.EventBus;

/**
 * Created by Michael Pi on 2016/10/19.
 */
public class WXEntryActivity extends Activity implements IWXAPIEventHandler {
    private IWXAPI api;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        api = WXAPIFactory.createWXAPI(this, "wx11168c360e45dc15", false);
        api.handleIntent(this.getIntent(), this);
    }

    @Override
    public void onReq(BaseReq baseReq) {

    }

    @Override
    public void onResp(BaseResp baseResp) {
        String code = null;
        String state = null;
        if (baseResp instanceof SendAuth.Resp) {
            //授权登录
            if (baseResp.errCode == BaseResp.ErrCode.ERR_OK) {
                code = ((SendAuth.Resp) baseResp).code;
                state = ((SendAuth.Resp) baseResp).state;
                Logger.d("微信授权code", code + "     " + baseResp.errCode);
            }
            EventBus.getDefault().post(new WxAuthEvent(baseResp.errCode, null != code ? code : "", null != state ? state : ""));
        } else if (baseResp instanceof SendMessageToWX.Resp) {
            //分享
        }
        finish();
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);//必须调用此句话
    }

}
